package com.cn.xact.ty_treasured_book.sqlite;

import java.util.List;

import com.cn.xact.ty_treasured_book.TYUserState;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class TYDBManager {
    private TYSqlliteHelper helper;
    private SQLiteDatabase db;

    public TYDBManager(Context context) {
        helper = new TYSqlliteHelper(context);
        db = helper.getWritableDatabase();
    }

    public void add(TYUserState user) {
        db.beginTransaction();
        try {
            db.execSQL("INSERT INTO person VALUES(null, ?, ?, ?)", new Object[]{user.getUserNick()
                                                                              , user.getUserLoginCount()
                                                                              , user.getUserCredit()
                                                                              , user.getUserRank()});
            db.setTransactionSuccessful();
        } finally {
            db.endTransaction();
        }
    }

    public void delete(TYUserState user) {
        db.delete("person", "nick == ?", new String[]{String.valueOf(user.getUserNick())});
    }

    public Cursor queryTheCursor() {
        Cursor c = db.rawQuery("SELECT * FROM person", null);
        return c;
    }

    public TYUserState getCurUser() {
        Cursor c = queryTheCursor();
        c.moveToFirst();
        TYUserState ret = new TYUserState(c.getString(c.getColumnIndex("nick")));
        return ret;
    }

    public void closeDB() {
        db.close();
    }
}